#!/usr/bin/env node

const log=console.log,
    {con}=require('./common.js');

async function getLogin(id,cipher){
    const sqlCount=`select count(id) count from user where id=${id};`;
    let [count]=await con.execute(sqlCount);
    if(count[0].count==1){
        const sql=`select * from user where id=${id};`;
        let [rows]=await con.execute(sql);
        const sqlcheck=`select cipher from user where id=${id};`;
        let [rowscheck]=await con.execute(sqlcheck);
        if(cipher!=rowscheck[0].cipher){
            return "密码错误"
        }
        return rows[0];
    }
    else{
        const newsql=`insert into user(id,cipher,phone,attention,fan,user.like) values("${id}","${cipher}","${id}",0,0,0)`;
        let [newrows]=await con.execute(newsql);
        const sql=`select * from user where id=${id};`;
        let [rows]=await con.execute(sql);
        log(rows[0])
        return rows[0];
    }    
}


module.exports={getLogin};